/**
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
*
* PmPurchasePaymentService
* 外包付款申请
*
* 修改纪录
* 2020-05-14 版本：1.0 shenyouyi 创建。
* @version 版本：1.0
* @author 作者：shenyouyi
* 创建日期：2020-05-14
*/
package com.topscomm.pm.service.impl;

import com.topscomm.pm.service.IPmProjectService;
import com.topscomm.pm.service.IPmPurchaseContractService;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.SessionUserBean;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.pm.service.IPmPurchasePaymentService;
import com.topscomm.pm.service.impl.auto.PmPurchasePaymentServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.pm.pojo.PmPurchasePaymentEntity;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "pmPurchasePaymentService")
@Scope(value = "singleton")
public class PmPurchasePaymentService extends PmPurchasePaymentServiceAuto implements IPmPurchasePaymentService {

	@Autowired
	private IPmProjectService pmProjectService;
	@Autowired
	private IPmPurchaseContractService pmPurchaseContractService;

	/**
	 * @author: shenyouyi
	 * @date: 2020-05-14
	 * @param entity
	 * @description:单号生成
	 * @modify:
	 */
	@Override
	protected void beforeInsert(BasicEntity entity){
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String docNo = String.valueOf(arrayId[0]);
			docNo = docNo.substring(0, 6) + docNo.substring(9);
			entity.setValue(PmPurchasePaymentEntity.FieldDocno, docNo);
		}
	}

	/**
	  * @author: shenyouyi
	  * @date: 2020/6/16
	  * @param
	  * @description: 重写取消审批后的方法
	  **/
	@Override
	@Transactional(rollbackFor = Exception.class)
	protected void afterDelApprove(TapEntity entity, SessionUserBean sessionUserBean) {
		Long id = entity.getId();
		this.updatePurchasepaymentmoneyOfProjectBase(id.toString(), false);
		super.afterDelApprove(entity, sessionUserBean);
	}

	/**
	  * @author: shenyouyi
	  * @date: 2020/6/16
	  * @param
	  * @description: 采购付款审批通过或者取消审批后执行的方法
	  **/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updatePurchasepaymentmoneyOfProjectBase(String sourceId, boolean approvalSuccess) {
		PmPurchasePaymentEntity pmPurchasePaymentEntity = this.queryById(sourceId);
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("projectid = {0} and state = {1} and id <> {2}", pmPurchasePaymentEntity.getProjectid(), TapSystemConst.TapApprovalBillStateEnum.Approved, sourceId);
		BigDecimal totalMoney = BigDecimal.ZERO;
		if(approvalSuccess){
			totalMoney = pmPurchasePaymentEntity.getMoney();
		}
		List<String> queryFields = new ArrayList<>();
//		queryFields.add("sum(money) as totalmoney");
		queryFields.add("sum(NVL(money,0)) as totalmoney");
		List<Map<String,Object>> moneyList = this.queryMapFieldsByWhere(sql.toString(), queryFields);
		if(moneyList.size()>0 && moneyList.get(0)!=null){
			BigDecimal partMoney =  (BigDecimal) moneyList.get(0).get("totalmoney");
			totalMoney = totalMoney.add(partMoney);
		}
		// 更新项目表中的采购付款金额
		sql.clear();
		sql.appendSingle("id = {0}",pmPurchasePaymentEntity.getProjectid());
		Map<String,Object> updateFields = new HashMap<>();
		updateFields.put("purchasepaymentmoney", totalMoney);
		pmProjectService.updateByWhere(updateFields, sql.toString());
		// 更新采购合同表中的已付款金额
		sql.clear();
		sql.appendSingle("purchasecontractid = {0} and state = {1} and id <> {2}", pmPurchasePaymentEntity.getPurchasecontractid(), TapSystemConst.TapApprovalBillStateEnum.Approved, sourceId);
		totalMoney = BigDecimal.ZERO;
		if(approvalSuccess){
			totalMoney = pmPurchasePaymentEntity.getMoney();
		}
		moneyList = this.queryMapFieldsByWhere(sql.toString(), queryFields);
		if(moneyList.size()>0 && moneyList.get(0)!=null){
			BigDecimal partMoney =  (BigDecimal) moneyList.get(0).get("totalmoney");
			totalMoney = totalMoney.add(partMoney);
		}
		updateFields.clear();
		updateFields.put("totalpaymoney", totalMoney);
		sql.clear();
		sql.appendSingle("id = {0}", pmPurchasePaymentEntity.getPurchasecontractid());
		pmPurchaseContractService.updateByWhere(updateFields, sql.toString());
	}
}
